import axios from 'axios'
import { Notification, MessageBox, Message } from 'element-ui'
import {getURLQueryString,setCookie,getCookie,baseUrl,apiBaseUrl,encryptDes} from '@/utils/common'
import qs from 'qs'
import Vue from 'vue'
// 创建axios实例
const service = axios.create({
	// axios中请求配置有baseURL选项，表示请求URL公共部分
	//baseURL: '/api',
	//baseURL:'//xnh.xnhkfpt.com/vueapi/api',//正式
	baseURL:location.host.includes('localhost')?'/api':location.host.includes('xnh.xnhkfpt.com')?'//xnh.xnhkfpt.com/vueapi/api':'//xnhloc.xnhkfpt.com/api',
	// 超时
	timeout: 60000
})
console.log(process.env.VUE_APP_BASE_API)
// request拦截器
service.interceptors.request.use(config => {
	var token = getCookie("token");
	if(token) {
		config.headers.Authorization = token;
	}

	// get请求映射params参数
//	if(config.method === 'get' && config.params) {
//		let url = config.url + '?';
//		for(const propName of Object.keys(config.params)) {
//			const value = config.params[propName];
//			var part = encodeURIComponent(propName) + "=";
//			if(value !== null && typeof(value) !== "undefined") {
//				if(typeof value === 'object') {
//					for(const key of Object.keys(value)) {
//						let params = propName + '[' + key + ']';
//						var subPart = encodeURIComponent(params) + "=";
//						url += subPart + encodeURIComponent(value[key]) + "&";
//					}
//				} else {
//					url += part + encodeURIComponent(value) + "&";
//				}
//			}
//		}
//		url = url.slice(0, -1);
//		config.params = {};
//		config.url = url;
//	}
	config.params={
		param:encryptDes(JSON.stringify({
			parampd:config.params||'',
			producttype:location.hash.match('poster')?'poster':sessionStorage.getItem('producttype')
		})),
		login_name:'sinontech_vue'
	}
	if(config.method==='post'){
		if(config.headers['Content-Type']&&config.headers['Content-Type'].includes('application/json')){
			return config
		}
//		if(config.headers['Content-Type']&&config.headers['Content-Type'].includes('multipart/form-data')){
//			config.params={
//				param:JSON.stringify({
//					parampd:config.params||'',
//					producttype:producttype
//				}),
//				login_name:'sinontech_vue'
//			}
//			return config
//		}
}
	return config
}, error => {
	console.log("request error:" + error);
	// return Promise.resolve({
	// 	code: "-1",
	// 	msg: "请求失败,请检查网络:" + error
	// });
})

// 响应拦截器
service.interceptors.response.use(function(response) {
		var data = response.data;
		if(!data) {
			Message({
				message: "返回数据异常",
				type: "error"
			});
		}
		if(data.code == "-10000"||data.code == "-10001") {
//			Message({
//				message: "当前token认证请求失败或已失效",
//				type: "error"
//			});
			 setTimeout(()=>{
//			 	setCookie('producttype','',-1)
			 	sessionStorage.removeItem('producttype')
			 	location.href =baseUrl+'/logout.do'
			 },1)
			return false
		}
		if(data.code == "-100009") {
			Message({
				message: "没有访问权限",
				type: "error"
			});
			return false
		}
		if(data) {
			return data;
		}
	},
	function(error) {
		console.log("response error:" + error);
		// return Promise.resolve({
		// 	code: "-1",
		// 	msg: "请求失败,请检查网络:" + error
		// });
	}
)
export default service